package com.situ.meetingRoom.dao.impl;

import com.situ.meetingRoom.dao.BoardroomDao;
import com.situ.meetingRoom.entity.Boardroom;
import com.situ.meetingRoom.entity.User;
import com.situ.meetingRoom.util.DBUtil;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.BoardroomVO;
import com.situ.meetingRoom.vo.UserVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BoardroomDaoImpl implements BoardroomDao {
    private DBUtil dbUtil = new DBUtil();
    @Override
    public List<BoardroomVO> selectAll(Boardroom boardroom) {
        String sql = "select * from boardroom where 1=1 ";
        List<Object> params = new ArrayList<>();
        if (boardroom.getName() != null && boardroom.getName().length() > 0) {
            sql += "and boardroom.name like ? ";
            params.add("%" + boardroom.getName() + "%");
        }
        if (boardroom.getName() != null && boardroom.getName().length() > 0) {
            sql += "and address like ? ";
            params.add("%" + boardroom.getAddress() + "%");
        }

        if (boardroom.getStatus() != null) {
            sql += "and boardroom.status = ? ";
            params.add(boardroom.getStatus());
        }

        return dbUtil.query(BoardroomVO.class, sql, params.toArray());
    }

    @Override
    public int insert(Boardroom boardroom) {
        String sql = "insert into boardroom (name,capacity,address) values (?,?,?)";
        return dbUtil.update(sql, boardroom.getName(),boardroom.getCapacity(),boardroom.getAddress());
    }

    @Override
    public Boardroom selectById(Integer id) {
        String sql = "select * from boardroom where id=?";
        return dbUtil.queryOne(Boardroom.class, sql, id);
    }

    @Override
    public int update(Boardroom boardroom) {
        String sql = "update boardroom " +
                "set ";
        List<Object> params = new ArrayList<>();

        if (boardroom.getName() != null && boardroom.getName().length() > 0) {
            sql += "name=?,";
            params.add(boardroom.getName());
        }
        if (boardroom.getCapacity() != null && boardroom.getCapacity().length() > 0) {
            sql += "capacity=?,";
            params.add(boardroom.getCapacity());
        }
        if (boardroom.getAddress() != null && boardroom.getAddress().length() > 0) {
            sql += "address=?,";
            params.add(boardroom.getAddress());
        }
        if (boardroom.getStatus() != null) {
            sql += "status=?,";
            params.add(boardroom.getStatus());
        }
        sql = sql.substring(0, sql.length() - 1);
        sql += " where id = ?";
        params.add(boardroom.getId());
        return dbUtil.update(sql, params.toArray());
    }

    @Override
    public PageInfo selectForPage(Integer page, Integer limit, Boardroom boardroom) {
        String sqlWhere = "";
        List<Object> params = new ArrayList<>();

        if (boardroom.getName() != null && boardroom.getName().length() > 0) {
            sqlWhere += " and name like ? ";
            params.add("%" + boardroom.getName() + "%");
        }

        if (boardroom.getAddress() != null && boardroom.getAddress().length() > 0) {
            sqlWhere += " and address like ? ";
            params.add("%" + boardroom.getAddress() + "%");
        }

        if (boardroom.getStatus() != null) {
            sqlWhere += "and status = ? ";
            params.add(boardroom.getStatus());
        }
        //记录的条数
        String sql = "select count(*) count from boardroom where 1=1 " + sqlWhere;
        List<Map<String, Object>> list1 = dbUtil.query(sql, params.toArray());
        Long count = (Long) list1.get(0).get("count");
        sql = "select * from boardroom where 1=1  " + sqlWhere +
                " limit ?,? ";

        params.add((page - 1) * limit);
        params.add(limit);

        List list = dbUtil.query(sql, params.toArray());

        PageInfo pageInfo = new PageInfo(count, list);

        return pageInfo;
    }

    @Override
    public Boardroom selectByName(String name) {
        String sql = "select * from boardroom where name = ?";
        return dbUtil.queryOne(Boardroom.class,sql,name);
    }

}
